package com.study.shengsiyuan.example01;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpObject;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.util.CharsetUtil;

public class TestHttpServerHandler extends SimpleChannelInboundHandler<HttpObject> {
	@SuppressWarnings("deprecation")
	@Override
	protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {
		if(msg instanceof HttpRequest) {
//			System.out.println("------执行请求");
			HttpRequest req =(HttpRequest)msg;

			System.out.println("请求方法名" + req.getMethod().name());
			System.out.println("请求路径" + req.getUri());

			ByteBuf content = Unpooled.copiedBuffer("Hello Word",CharsetUtil.UTF_8);
			FullHttpResponse resp = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK,content);
			resp.headers().set(HttpHeaderNames.CONTENT_TYPE,"text/palin")
			.set(HttpHeaderNames.CONTENT_LENGTH,content.readableBytes());
			ctx.writeAndFlush(resp);
			//可以判断客户端http的版本号 然后做一些操作再关闭
			ctx.channel().close();//关闭客户端连接
		}
	}


	@Override
	public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
		System.out.println("---------channelRegistered");
		super.channelRegistered(ctx);

	}

	@Override
	public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
		System.out.println("---------channelUnregistered");
		super.channelUnregistered(ctx);
	}

	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		System.out.println("---------channelActive");
		super.channelActive(ctx);
	}

	@Override
	public void channelInactive(ChannelHandlerContext ctx) throws Exception {
		System.out.println("---------channelInactive");
		super.channelInactive(ctx);
	}

	@Override
	public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
		System.out.println("---------channelReadComplete");
		super.channelReadComplete(ctx);
	}

	@Override
	public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
		System.out.println("---------userEventTriggered");
		super.userEventTriggered(ctx, evt);
	}

	@Override
	public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
		System.out.println("---------channelWritabilityChanged");
		super.channelWritabilityChanged(ctx);
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		System.out.println("---------exception");
		super.exceptionCaught(ctx, cause);
	}


	@Override
	public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
		System.out.println("---------handlerAdded");
		super.handlerAdded(ctx);
	}


	@Override
	public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
		System.out.println("---------handlerRemoved");
		super.handlerRemoved(ctx);
	}


}
